var picCode;
$(function () {
    var ctx = "[[@{/}]]";
    layui.use(['form', 'layer'], function () {
        var path = window.location.href;
        //console.info("==请求的uri:"+path);
        if (path.indexOf("kickout") > 0) {
            layer.msg('您的账号已在别处登录；若不是您本人操作，请立即修改密码', { icon: 7, offset:"rt", time: 2000, shade: [0.6, '#000', true] });
            // layer.alert('您的账号已在别处登录；若不是您本人操作，请立即修改密码！');
        }
        // else {
        //     layer.msg('会话超期或未登录，请重新登陆！', { icon: 7, offset:"rt", time: 2000, shade: [0.6, '#000', true] });
        //     // layer.alert("会话超期或未登录，请重新登陆！")
        // }
        var form = layui.form;
        form.on("submit(login)", function () {
            login();
            return false;
        });
        form.on('submit(mmSubmit)', function () {
            var loginname = $("#loginname").val()
            var oldvalue = $("#oldmm").val();
            var newvalue = $("#newmm").val();
            var revalue = $("#remm").val();
            var pwdPattern = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,16}$/;

            if (loginname == null){
                layer.msg('用户名不能为空', { icon: 7, time: 2000, shade: [0.6, '#000', true] });
                return
            }

            if (!pwdPattern.test(newvalue)){
                layer.msg('密码长度至少6位，由字母大小写，数字和符号组成', { icon: 7, time: 2000, shade: [0.6, '#000', true] });
                return
            }

            if (newvalue != revalue){
                layer.msg('新密码和确认密码不一致', { icon: 7, time: 2000, shade: [0.6, '#000', true] });
                return
            }

            $.ajax({
                type: "PUT",
                data: {
                    "oldpassword": userEncrypt(oldvalue),
                    "newpassword": userEncrypt(newvalue),
                    "confirmpassword": userEncrypt(revalue),
                    "loginname": userEncrypt(loginname)
                },
                contentType: 'application/x-www-form-urlencoded',
                url: ctxPath + "/system/user/modifyPassword",
                success: function (data) {
                    if (data.code == "1") {
                        layer.alert("修改成功", function () {
                            layer.closeAll();
                            // window.location.reload()
                        });
                    } else {
                        layer.alert(data.message)
                    }
                }
            });
        });
    })
})
$(window).on("load", function () {
    //绘制验证码图片
    picCode = drawPic();
});

$(document).keydown(function (event) {
    if (event.keyCode == 13) {
        login()
    }
});

function login() {
    var flag = checkPicCode();
    if (flag != false) {
        $.post("/system/user/login",
            {
                "loginname": userEncrypt($("#loginname").val()),
                "password": userEncrypt($("#password").val()),
                "piccode": userEncrypt($("#piccode").val()),
                "rememberMe": $("#rememberMe").checked
            }, function (data) {
                if (data.code == "1") {
                    window.location.href = '/';
                } else if (data.code == "3") {
                    layer.open({
                        type: 1,
                        title: data.message,
                        fixed: false,
                        resize: false,
                        shadeClose: true,
                        area: ['450px'],
                        content: $('#modifyPWD')
                    })
                } else {
                    layer.alert(data.message, function () {
                        layer.closeAll();
                    });
                    picCode = drawPic();
                    $("#code").val("");
                }
            });
    } else {
        drawPic();
    }
}

//校验及验证码
function checkPicCode() {
    var code = $("#piccode").val();
    if (picCode.toLowerCase() != code.toLowerCase()) {
        //tips层-右
        layer.tips("请您输入正确的验证码", '#canvas', {
            tips: [2, '#78BA32'], //还可配置颜色
            tipsMore: true
        });
        return false;
    }
}